package demo1;

import java.util.Arrays;
import java.util.Comparator;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 30318
 * Date: 2023-05-21
 * Time: 9:10
 */
class Student implements Comparable<Student>{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        if(this.age - o.age > 0){
            return 1;
        }else if(this.age - o.age < 0){
            return -1;
        }else {
            return 0;
        }
        //return 0;
    }
}

class AgeComparator implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}

class NameComparator implements Comparator<Student>{


    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}

public class Test {

    public static void bubbleSort(Comparable[] array){
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j].compareTo(array[j+1]) > 0){
                    Comparable tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                }
            }
        }
    }

    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan", 15);
        students[1] = new Student("lisi", 18);
        students[2] = new Student("wangwu", 14);

        bubbleSort(students);

        System.out.println(Arrays.toString(students));
        
    }

    public static void main5(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan", 15);
        students[1] = new Student("lisi", 18);
        students[2] = new Student("wangwu", 14);

        //AgeComparator ageComparator = new AgeComparator();
        NameComparator nameComparator = new NameComparator();
        Arrays.sort(students, nameComparator);


        System.out.println(Arrays.toString(students));
    }

    public static void main4(String[] args) {
        Student student1 = new Student("张三", 15);
        Student student2 = new Student("李四", 18);

        AgeComparator ageComparator = new AgeComparator();

        if(ageComparator.compare(student1,student2) > 0){
            System.out.println("student1 > student2");
        }else{
            System.out.println("student1 < student2");
        }
    }

    public static void main2(String[] args) {
        String[] strings = {"abc", "hello", "world"};
        Arrays.sort(strings);
        System.out.println(Arrays.toString(strings));
    }
    public static void main3(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("张三", 15);
        students[1] = new Student("李四", 18);
        students[2] = new Student("王五", 14);

        Arrays.sort(students);

        System.out.println(Arrays.toString(students));
    }

    public static void main1(String[] args) {
        int[] array = {1,4,2,7,3,8,5};
        System.out.println(Arrays.toString(array));
    }
}
